﻿using System.Collections;

/// <summary>
/// 游戏状态
/// </summary>
public class GameState
{

	protected bool _reconnecting;

	/// <summary>
	/// 进入状态
	/// </summary>
	public virtual void Enter ()
	{
		_reconnecting = false;
	}

	/// <summary>
	/// 离开状态
	/// </summary>
	public virtual void Leave ()
	{

	}

	/// <summary>
	/// 处理消息
	/// </summary>
	/// <param name="message"></param>
	/// <returns></returns>
	public virtual bool HandleMessage (BaseProceduar.Message message)
	{
		switch (message.msgId) {
		case Commands.CMD_MOD_MSG:
			switch (message.iParam) {
			case GameCommands.CMD_RECONNECT:
				_reconnecting = true;
				UIRootController.inst.ShowMask (true);
				NetIOController.inst.isGame = false;
				NetIOController.inst.ConnectToNewURL (Constants.LOGIN_URL);
				return true;
			default:
				break;
			}
			break;
		}

		return false;
	}

	/// <summary>
	/// 处理消息
	/// </summary>
	/// <param name="message"></param>
	/// <returns></returns>
	public virtual bool ProcessPacket (IOMessage message)
	{
		switch (message.code) {
		case IOCmds.WS_CONN_OPEN:
			// 与服务器的连接打开
			UIRootController.inst.ShowMask (false);
			if (_reconnecting) {
				UIGameReconnectWindow.CloseWindow ();
				GameController.inst.SwitchTo (GameProcedure.inst);
			}
			return true;

		case IOCmds.WS_CONN_CLOSED:
			DebugInfo.Warning ("未知的网络消息：" + message.ToJSON ());
			return true;
		}

		return false;
	}

	/// <summary>
	/// 请求加入房间
	/// </summary>
	protected void RequestJoinRoom ()
	{
		Hashtable reqArgs = new Hashtable ();
		reqArgs ["roomId"] = GameDataKernel.inst.roomId;
		reqArgs ["uid"] = DataKernel.inst.userInfo.uid;
		reqArgs ["headpic"] = DataKernel.inst.userInfo.headpic;
		reqArgs ["name"] = DataKernel.inst.userInfo.name;
		reqArgs ["sex"] = DataKernel.inst.userInfo.sex;

		UIRootController.inst.ShowMask (true);
		NetIOController.inst.SendMessage (new IOMessage (IOCmds.CMSG_JOIN_ROOM, IOStates.STATE_OK, new HashtableEx (reqArgs)));
	}
}
